package com.foreveross.crawl.application.impl.computer;

import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.foreveross.crawl.application.computer.AbstratComputerInfo;
import com.foreveross.crawl.vo.MachineInfo;
import com.foreveross.crawl.vo.Message;
import com.foreveross.mq.message.Command;
import com.sun.management.OperatingSystemMXBean;

public class ComputerInstanceByStrategy {

	private AbstratComputerInfo compurterInfo;
	
	private ComputerInstanceByStrategy() throws Exception{
		String osType = this.getOSType();
		if(osType == null || osType.equals("")){
			throw new Exception("获取操作系统类型失败！");
		}
		if(osType.toLowerCase().contains("windows")){
			compurterInfo = new ComputerInfoByWindowsApplicationImpl();
		} else if(osType.toLowerCase().contains("linux")){
			compurterInfo = new ComputerInfoByLinuxAppicationImpl();
		} else {
			throw new Exception("对不起，暂时不支持该操作系统!");
		}
	}
	
	public static ComputerInstanceByStrategy getInstance() throws Exception{
		return new ComputerInstanceByStrategy();
	}
	
	public Message getComputerMessage() throws Exception{
		
		Message message = new Message();
		message.setCommand(Command.REGISTER);
		List<MachineInfo> infoList = new ArrayList<MachineInfo>();
		infoList.add(this.setMachineInfo());
		message.setBody(infoList);
		return message;
	}
	
	/**  
	 * @Description: 设置机器信息
	 * @return
	 * @author luofangyi
	 * @throws Exception 
	 * @date 2014-6-27 下午5:20:20 
	 */ 
	private MachineInfo setMachineInfo() throws Exception{
		int kb = 1024;
		// 可使用内存
        long totalMemory = Runtime.getRuntime().totalMemory() / kb;
        // 剩余内存
        long freeMemory = Runtime.getRuntime().freeMemory() / kb;
        // 最大可使用内存
        long maxMemory = Runtime.getRuntime().maxMemory() / kb;
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
                .getOperatingSystemMXBean();
        // 总的物理内存
        long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;
        // 剩余的物理内存
        long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb;
        // 已使用的物理内存
        long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb
                .getFreePhysicalMemorySize())
                / kb;
        // 获得线程总数
        ThreadGroup parentThread;
        for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
                .getParent() != null; parentThread = parentThread.getParent())
            ;
        int totalThread = parentThread.activeCount();
        
        MachineInfo info = new MachineInfo();
		compurterInfo.initInetAddress();
		info.setIp(compurterInfo.getIpAddress());
		info.setMacAddress(compurterInfo.getMACAddress());
		info.setOsType(getOSType());
		info.setOsArch(getOSArch());
		info.setOsVersion(getOSVersion());
//		info.setCpuRatio(compurterInfo.getCpuRate());		
		info.setTotalMemory(totalMemory);
		info.setFreeMemory(freeMemory);
		info.setMaxMemory(maxMemory);
		info.setTotalMemorySize(totalMemorySize);
		info.setFreePhysicalMemorySize(freePhysicalMemorySize);
		info.setUsedMemory(usedMemory);
		info.setTotalThread(totalThread);
		
		return info;
	}
	
	/**
	 * 返回系统属性集
	 * @return
	 */
	public Properties getProperties(){
		return System.getProperties();
	}
	
	/**
	 * 根据属性key获得值
	 * @param key
	 * @return
	 */
	private String getPropertyByKey(String key){
		return this.getProperties().getProperty(key);
	}
	
	/**
	 * 获取当前操作系统
	 * @return
	 */
	private String getOSType(){
		return this.getPropertyByKey("os.name");
	}
	
	/**
	 * 操作系统构架
	 * @return
	 */
	private String getOSArch(){
		return this.getPropertyByKey("os.arch");
	}
	
	private String getOSVersion(){
		return this.getPropertyByKey("os.version");
	}
}
